package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.dto.returnPlan.ReturnPlanProgressDto;
import cn.com.cifi.mars.bean.vo.returnPlan.returnReport.ReturnPlanProgressVo;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.constant.VersionCreateEnum;
import cn.com.cifi.mars.constant.VersionStatusEnum;
import cn.com.cifi.mars.constant.VersionTypeEunm;
import cn.com.cifi.mars.entity.ReturnPlanProgressBase;
import cn.com.cifi.mars.mapper.mars.ReturnPlanProgressBaseMapper;
import cn.com.cifi.mars.service.ReturnPlanProgressBaseService;
import cn.com.cifi.mars.service.SignPlanVersionService;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import cn.com.cifi.mars.util.StrUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回款计划进度表基础表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2021-08-25
 */
@Service
public class ReturnPlanProgressBaseServiceImpl extends ServiceImpl<ReturnPlanProgressBaseMapper, ReturnPlanProgressBase> implements ReturnPlanProgressBaseService {

    @Autowired
    private ReturnPlanProgressBaseMapper returnPlanProgressBaseMapper;
    @Autowired
    private SignPlanVersionService signPlanVersionService;

    @Override
    @Transactional
    public void refresh() {
        List<ReturnPlanProgressBase> baseList = returnPlanProgressBaseMapper.selectBaseList();
        List<ReturnPlanProgressBase> resList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(baseList)) {
            for (ReturnPlanProgressBase obj : baseList) {
                if (!signPlanVersionService.getStageIsSaly(obj.getStageId())) {
                    continue;
                }
                resList.add(obj);
            }
            returnPlanProgressBaseMapper.deleteAll();
            this.saveBatch(resList);
        }
    }

    @Override
    public List<ReturnPlanProgressVo> getReturnPlanProgressList(String yearMonth, List<String> projectIdList,
                                                                String name, String versionStatus, String startTime, String endTime) {
        List<ReturnPlanProgressVo> resList = new ArrayList<>();
        //包含审查时间条件时，只查询审批已通过项目
        if (StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)) {
            //包含审查时间条件时，版本状态不为空且不是“审批已通过”，不显示数据
            if (StringUtils.isEmpty(versionStatus) || StringUtils.equalsIgnoreCase(versionStatus, VersionStatusEnum.SUCCESSAPPROVED.getKey())) {
                versionStatus = VersionStatusEnum.SUCCESSAPPROVED.getKey();
            } else {
                return resList;
            }
            //编辑时间格式
            startTime = startTime != null ? new StringBuffer(startTime).insert(10, " ").toString() : null;
            endTime = endTime != null ? new StringBuffer(endTime).insert(10, " ").toString() : null;
        }

        List<ReturnPlanProgressDto> list = returnPlanProgressBaseMapper.selectDataList(yearMonth, projectIdList, versionStatus, startTime, endTime, name);
        if (CollectionUtils.isNotEmpty(list)) {
            for (ReturnPlanProgressDto dto : list) {
                ReturnPlanProgressVo vo = new ReturnPlanProgressVo();
                vo.setBusinessDivisionName(dto.getBusinessDivisionName());
                vo.setCityCompanyName(dto.getCityCompanyName());
                vo.setProjectCode(dto.getProjectCode());
                vo.setProjectName(dto.getProjectName());
                vo.setStageCode(dto.getStageCode());
                vo.setStageName(dto.getStageName());
                if (StrUtils.isEmpty(dto.getVersionType())) {
                    vo.setReturnPlanVersionName("无");
                } else {
                    vo.setReturnPlanVersionName(getVersionName(dto.getVersionType(), dto.getVersionNum(), dto.getVersionState(), dto.getAffiliationMonth(),
                            dto.getVersionIdentify(), dto.getIsAssessment(), dto.getUpdateTime(), dto.getCreateTime()));
                }
                if (StrUtils.isEmpty(dto.getVersionState())) {
                    vo.setVersionStateName("未编制");
                } else {
                    vo.setVersionStateName(VersionStatusEnum.getValueByKey(dto.getVersionState()));
                }
                vo.setCreateByName(dto.getCreateByName());
                vo.setCreateTime(dto.getCreateTime());
                vo.setVersionCreateName(VersionCreateEnum.getKeyByValue(dto.getVersionCreate()));

                if (VersionStatusEnum.SUCCESSAPPROVED.getKey().equals(dto.getVersionState())) {
                    vo.setSuccessApprovalTime(dto.getUpdateTime());
                }

                if (StrUtils.isEmpty(dto.getSourceVersionType())) {
                    vo.setReturnPlanVersionName("");
                } else {
                    vo.setSourceSignVersionName(getVersionName(dto.getSourceVersionType(), dto.getSourceVersionNum(), dto.getSourceVersionState(), dto.getSourceAffiliationMonth(),
                            dto.getSourceVersionIdentify(), dto.getSourceIsAssessmentVersion(), dto.getSourceUpdateTime(), dto.getSourceCreateTime()));
                }

                resList.add(vo);
            }
        }

        return resList;
    }


    public static String getVersionName(String versionType, String versionNum, String versionState, String affiliationMonth,
                                        String versionIdentify, Integer isAssessment, LocalDateTime updateTime, LocalDateTime createTime) {
        if (StrUtils.isEmpty(versionType)) {
            return "无";
        }
        String versionName = "";
        String versionTypeName = VersionTypeEunm.getValueByKey(versionType);
        //最后更新时间
        if (updateTime == null) {
            updateTime = createTime;
        }
        //最后更新时间年-月-日
        String updateName = LocalDateTimeUtils.formatTime(updateTime, "yyyyMMdd");
        if (versionType.equals(VersionTypeEunm.BUDGET.getKey())) {
            versionName = versionIdentify + versionNum;
        } else if (versionType.equals(VersionTypeEunm.DYNAMIC.getKey())) {
            String yearMonth = affiliationMonth.replace("-", "");
            versionName = yearMonth + "月" + versionTypeName + versionNum;
        } else {
            versionName = versionTypeName + versionNum;
        }


        if (versionState.equals(VersionStatusEnum.SUCCESSAPPROVED.getKey()) || versionState.equals(VersionStatusEnum.SYSGENERATION.getKey())) {
            versionName = versionName + "-" + updateName;
            if (isAssessment != null && isAssessment.equals(IsDeleteEnum.YES.getKey())) {
                versionName = versionName + "(考核版)";
            }
        } else {
            if (isAssessment != null && isAssessment.equals(IsDeleteEnum.YES.getKey())) {
                versionName = versionName + "(考核版)";
            }
        }
        return versionName;
    }

}
